﻿using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Data;

namespace Gp.Scripts.Core {
    public class AttackActionResult : IActionResult {
        /// <summary>
        /// 是否命中
        /// </summary>
        public bool IsHit = true;


        /// <summary>
        /// 造成的伤害
        /// </summary>
        public int DamageMake = 0;
        
        /// <summary>
        /// 对生命值造成的伤害
        /// </summary>
        public int HealthDamageMake = 0;

        /// <summary>
        /// 伤害减少（来源于机制）
        /// </summary>
        public int DamageReduce = 0;


        public DamageType DamageType = DamageType.Physic;



        public readonly List<ArmorDamageInfo> ArmorDamageInfos = new();

        /// <summary>
        /// 在攻击后，攻击目标会消耗的buff
        /// </summary>
        public readonly List<BaseBuff> BuffConsume;


        public AttackActionRequest Request { get; }
        public bool Blocked { get; set; }


        /// <summary>
        /// 受击者护甲值
        /// </summary>
        public int Defence = 0;



        private int _armorPointBeforeSettlement;

        public AttackActionResult(AttackActionRequest request) {
            Request = request;
            _armorPointBeforeSettlement = 1;
            BuffConsume = request.BuffConsume.ToList();
        }


        public bool WillArmorBreak() {
            var armorPoint = _armorPointBeforeSettlement;
            // 由于对于单块护甲板的伤害不会大于该护甲板当前耐久
            // 故 当前护甲值 <= 对护甲伤害
            return armorPoint > 0 && armorPoint <= ArmorDamageMake;
        }


        public int MoraleDamage {
            get {
                var target = Request.Target;
                var fixValue = (target.MaxHealth.Value + target.ComEquip.MaxArmorPoints()) *
                               target.ComCost.GetMaxValue(CostField.Morale);
                int baseValue = (int) (DamageMake * .1f + DamageMake / (float) fixValue);


                var bonusValue = 0;

                // 如果当此攻击使单位护甲值耗尽，单位士气额外减少10点
                if (WillArmorBreak()) {
                    bonusValue += 10;
                }


                var amount = bonusValue + baseValue;

                // //     惊骇: 拥有此效果时造成的士气伤害翻倍。
                // if (target.ComBuff.HasBuff<HorrorEffect>()) {
                //     amount *= 2;
                // }

                return amount;
            }
        }




        /// <summary>
        /// 实际造成的伤害。为护甲伤害之和 + 生命值伤害
        /// </summary>
        /// <returns></returns>
        public int ActualDamageMake => ArmorDamageMake + HealthDamageMake;


        public int ArmorDamageMake => ArmorDamageInfos.Sum(x => x.Damage);
        
        
        
        
        public override string ToString() {
            return
                $"[命中]：{IsHit}\n" +
                $"[最终伤害]：{DamageMake}\n" +
                $"[对生命伤害]：{HealthDamageMake}\n" +
                $"[护甲值]：{Defence}\n";
        }
    }
}